<template>
  <div class="clone">
    <div class="title">
      <span>店铺克隆</span>
    </div>
    <div class="cont-wrap">
      <span class="space">1.删除需复制数据店铺的原有数据</span>
      <div class="item">
        <!-- 不再提供手动删除 -->
        <span>选择店铺：</span>
        <!-- <input placeholder="店铺id" v-model="storeIdForDel" type="text" /> -->
        <el-select
          style="width:250px"
          v-model="storeIdForDel"
          filterable
          remote
          @change="changeStoreForClone"
          reserve-keyword
          placeholder="请输入店铺名后选择店铺"
          :remote-method="remoteMethod"
          :loading="loading">
          <el-option
            v-for="item in stores"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </div>
      <el-button style="margin:10px 0;" @click="del">删除</el-button>
      <span class="space">2.复制数据</span>
      <div class="item">
        <span>从哪个店铺复制：</span>
        <el-select
          style="width:250px"
          v-model="fromStoreId"
          filterable
          remote
          reserve-keyword
          placeholder="请输入店铺名后选择店铺"
          :remote-method="remoteMethod"
          :loading="loading">
          <el-option
            v-for="item in stores"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
        <!-- <input placeholder="请输入从哪个店铺id复制" v-model="fromStoreId" type="text" /> -->
      </div>
      <div class="item" style="margin-top:20px;">
        <span>复制到哪个店铺：</span>
        <el-select
          style="width:250px"
          v-model="toStoreId"
          filterable
          remote
          reserve-keyword
          placeholder="请输入店铺名后选择店铺"
          :remote-method="remoteMethod"
          :loading="loading">
          <el-option
            v-for="item in stores"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
        <!-- <input placeholder="请输入复制到店铺id" v-model="toStoreId" type="text" /> -->
      </div>
      <div class="item">
        <el-button style="margin:10px 0" @click="save">提交</el-button>
      </div>
      <span class="space">3.更新网费（复制后的店铺需更新网费）</span>
      <div class="item">
        <span>选择店铺：</span>
        <el-select
          style="width:250px"
          v-model="storeIdForUpdateNet"
          filterable
          remote
          reserve-keyword
          placeholder="请输入店铺名后选择店铺"
          :remote-method="remoteMethod"
          :loading="loading">
          <el-option
            v-for="item in stores"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
        <!-- <input placeholder="店铺id" v-model="storeIdForUpdateNet" type="text" /> -->
      </div>
      <div class="item">
        <el-button style="margin:10px 0;" @click="update">更新</el-button>
      </div>
    </div>
    <!--  -->
    <div class="title">
      <span>租赁商品克隆</span>
    </div>
    <div class="cont-wrap">
      <span class="space">复制租赁商品</span>
      <div class="item">
        <span>将从哪个店铺复制租赁商品：</span>
        <el-select
          style="width:250px"
          v-model="fromStoreIdForRent"
          filterable
          remote
          reserve-keyword
          placeholder="请输入店铺名后选择店铺"
          :remote-method="remoteMethod"
          :loading="loading">
          <el-option
            v-for="item in stores"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </div>
      <div class="item" style="margin-top:20px;">
        <span>复制租赁商品到哪个店铺：</span>
        <el-select
          style="width:250px"
          v-model="toStoreIdForRent"
          filterable
          remote
          reserve-keyword
          placeholder="请输入店铺名后选择店铺"
          :remote-method="remoteMethod"
          :loading="loading">
          <el-option
            v-for="item in stores"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
        <!-- <input placeholder="请输入复制到店铺id" v-model="toStoreId" type="text" /> -->
      </div>
      <div class="item">
        <el-button style="margin:10px 0" @click="saveForRent">提交</el-button>
      </div>
    </div>
    <!--  -->
    <div class="title">
      <span>订单状态修改</span>
    </div>
    <div class="cont-wrap">
      <span class="space">1.请输入订单号</span>
      <div class="item">
        <span>订单id：</span>
        <input placeholder="订单id" v-model="orderIdForStatus" type="text" /><button style="margin-left:10px;" @click.stop="getStatus">查看订单状态</button>
        <!--  -->
        <div style="color:green;padding-top:10px;" v-if="orderInfo">订单支付状态:{{ orderStatusTypeFormat[orderInfo.status] }} <span style="padding-left: 5px;" v-if="orderInfo && orderInfo.paymentTime">支付时间:{{ orderInfo.paymentTime | dateForGradePage }}</span></div>
        <!--  -->
        <span class="space">2.请选择想要修改成的订单状态</span>
        <select v-model="orderStatus" name="" id="">
          <option v-for="(item,index) in orderStatusTypes" :key="index" :value="item.value">{{item.name}}</option>
        </select>
      </div>
      <button @click="sureChangeOrderStatus()" style="margin-top:10px;">确定修改订单状态</button>
    </div>
    <!--  -->
    <div class="title" style="margin-top:10px;">
      <span>订单支付方式修改</span>
    </div>
    <div class="cont-wrap">
      <span class="space">1.请输入订单号或者流水号（二者填其一）</span>
      <div class="item">
        <span>订单id：</span>
        <input placeholder="订单id" v-model="orderId" type="text" /> 或 <input placeholder="流水号" v-model="orderCode" type="text" /><br>
        <span class="space">2.请选择想要修改成的支付方式</span>
        <select v-model="payType" name="" id="">
          <option v-for="(item,index) in payTypes" :key="index" :value="item.value">{{item.name}}</option>
        </select>
      </div>
      <button @click="sureChangeOrderPayType()" style="margin-top:10px;">确定修改</button>
    </div>
    <!--  -->
    <div class="title">
      <span>批处理订单</span>
    </div>
    <div class="cont-wrap">
      <span>开始时间：</span>
      <el-date-picker v-model="startOrderTime" type="datetime" placeholder="选择日期">
      </el-date-picker>
      <span>结束时间：</span>
      <el-date-picker v-model="endOrderTime" type="datetime" placeholder="选择日期">
      </el-date-picker>
      <button @click="sureDoOrder()" style="margin-top:10px;margin-left:10px;">确定</button>
    </div>
    <!--  -->
    <div class="title">
      <span>店主手机号修改</span>
    </div>
    <div class="cont-wrap">
      <span class="space">1.请输入店主的原手机号</span>
      <div class="item">
        <span>原手机号：</span>
        <input placeholder="原手机号" v-model="originPhone" type="text" />
      </div>
      <br>
      <span class="space">2.请输入店主现在的手机号</span>
      <div class="item">
        <span>现手机号：</span>
        <input placeholder="现手机号" v-model="nowPhone" type="text" />
      </div>
      <br>
      <span class="space">3.请输入店铺id</span>
      <div class="item">
        <span>店铺id：</span>
        <input placeholder="店铺id" v-model="changePhoneStoreId" type="text" />
      </div>
      <button @click="sureChangeStorePhone()" style="margin-top:10px;">确定修改</button>
    </div>
    <!--  -->
    <!-- https://prod-api.huoxingy.com/v1/call/mySelf/refund?mchId=399241224682434&orderNum=e86b186024aef24e4071a23841b55fea09fc&amount=64 -->
    <div class="title">
      <span>订单退款</span>
    </div>
    <div class="cont-wrap">
      <span class="space">1.请输入商户编号</span>
      <div class="item">
        <span>商户编号：</span>
        <input placeholder="商户编号" v-model="refundForm.mchId" type="text" />
      </div>
      <br>
      <span class="space">2.请输入订单号</span>
      <div class="item">
        <span>订单号：</span>
        <input placeholder="订单号" v-model="refundForm.orderNum" type="text" />
      </div>
      <br>
      <span class="space">3.请输入退款金额</span>
      <div class="item">
        <span>退款金额：</span>
        <input placeholder="退款金额" v-model="refundForm.amount" type="text" />
      </div>
      <button @click="sureRefund()" style="margin:10px 0;">确定退款</button>
    </div>
    <!--  -->
  </div>
</template>

<script>
import imgCrop from "./../components/imgCrop/imgCrop.vue";
import {BASE_URL} from "./../../urlConfig/index";

export default {
  components: { imgCrop },
  data() {
    return {
      refundForm:{
        mchId:"",
        orderNum:"",
        amount:"",
      },
      orderInfo:null,
      stores: [],
      loading: false,
      // 
      startOrderTime:"",
      endOrderTime:"",
      changePhoneStoreId:"",
      originPhone:"",
      nowPhone:"",
      payType:"",
      orderIdForStatus:"",
      orderStatus:"",
      orderStatusTypeFormat:{
        INIT:"初始",
        UNSETTLED:"未结算",
        UNFINISHED:"待处理",
        FINISHED:"完成",
        CANCELED:"取消",
        REFUNDED:"退款",
        NOTSURE:"支付中",
        FAIL:"失败",
      },
      orderStatusTypes:[
        {name:"初始",value:"INIT"},
        {name:"未结算",value:"UNSETTLED"},
        {name:"待处理",value:"UNFINISHED"},
        {name:"完成",value:"FINISHED"},
        {name:"取消",value:"CANCELED"},
        {name:"退款",value:"REFUNDED"},
        {name:"支付中",value:"NOTSURE"},
        {name:"失败",value:"FAIL"},
      ],
      payTypes:[
        {name:"现金",value:"CASH"},
        {name:"支付宝",value:"pay.alipay.micropay"},
        {name:"微信",value:"pay.wechat.micropay"},
        {name:"银联",value:"pay.unionpay.micropay"},
        {name:"京东",value:"pay.jdpay.micropay"},
        {name:"QQ",value:"pay.qq.micropay"},
        {name:"会员卡",value:"pay.shiming.micropay"},
        {name:"翼支付",value:"pay.bestpay.micropay"},
        {name:"会员支付",value:"MEMBER"},
      ],
      orderId:undefined,
      orderCode:undefined,
      fromStoreId: "",
      toStoreId: "",
      storeIdForDel: "",
      storeIdForUpdateNet: "",
      fromStoreIdForRent:"",
      toStoreIdForRent:"",
    };
  },
  methods: {
    changeStoreForClone(){
      this.toStoreId = this.storeIdForDel;
      this.storeIdForUpdateNet = this.storeIdForDel;
      this.$forceUpdate();
    },
    remoteMethod(query) {
      if (query !== '') {
        this.loading = true;
        this.$http({
          url:BASE_URL+"v1/call/get-store",
          method:"GET",
          params:{
            name:query
          }
        }).then(res=>{
          this.stores = res.data;
          this.loading = false;
        })
      } else {
        this.stores = [];
      }
    },
    // 
    sureRefund(){
      if(!this.refundForm.mchId || !this.refundForm.orderNum || !this.refundForm.amount){
        this.$message({
            message: "请填写完整信息",
            type: "error"
        });
        return;
      }
      this.$confirm(`确认将订单号(${this.refundForm.orderNum})进行退款:${this.refundForm.amount}元？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).then(() => {
          console.log("this.refundForm",this.refundForm)
          this.$http({
              url:BASE_URL+"v1/call/mySelf/refund",
              method:"GET",
              params:{
                ...this.refundForm
              }
          }).then(res=>{
              this.$message({
                  message: "操作成功",
                  type: "success"
              });
          })
      })
    },
    // 
    sureDoOrder(){
      if(!this.startOrderTime || !this.endOrderTime){
        this.$message({
            message: "看一下，是不是时间没选对！",
            type: "error"
        });
        return;
      }
      this.$confirm('确认此操作？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).then(() => {
          this.$http({
              url:BASE_URL+"v1/call/deal-mq",
              method:"GET",
              params:{
                  begin:new Date(this.startOrderTime).Format("yyyy-MM-dd hh:mm:ss"),
                  end:new Date(this.endOrderTime).Format("yyyy-MM-dd hh:mm:ss"),
              }
          }).then(res=>{
              this.$message({
                  message: "操作成功",
                  type: "success"
              });
          })
      })
    },
    sureChangeStorePhone(){
      if(!this.changePhoneStoreId || !this.originPhone || !this.nowPhone){
        this.$message({
          message: "请把信息填写完整！！！",
          type: "error"
        });
        return;
      }
      this.$http({
        url:BASE_URL+"v1/call/merge-store-mobile",
        method:"GET",
        params:{
          source:this.originPhone,
          target:this.nowPhone,
          storeId:this.changePhoneStoreId
        }
      }).then(res=>{
        if(res.data.code == 200){
          this.$message({
            message: res.data.msg,
            type: "success"
          });
        }else if(res.data.code == 400){
          this.$message({
            message: res.data.msg,
            type: "error"
          });
        }
      });
    },
    getStatus(){
      if(!this.orderIdForStatus){
        this.$message({
          message: "请填写订单号",
          type: "error"
        });
        return;
      }
      this.orderInfo = null;
      this.$http({
        url:BASE_URL+"v1/call/order/status",
        method:"GET",
        params:{
          orderId:this.orderIdForStatus,
        }
      }).then(res=>{
        console.log(res);
        this.orderInfo = res.data;
      })
    },
    sureChangeOrderStatus(){
      if(!this.orderIdForStatus){
        this.$message({
          message: "请填写订单号",
          type: "error"
        });
        return;
      }
      if(!this.orderStatus){
        this.$message({
          message: "请选择订单状态",
          type: "error"
        });
        return;
      }
      this.$http({
        url:BASE_URL+"v1/call/replay/order/status",
        method:"GET",
        params:{
          orderId:this.orderIdForStatus,
          orderStatus:this.orderStatus
        }
      }).then(res=>{
        this.$message({
          message: "操作成功",
          type: "success"
        });
      });
    },
    sureChangeOrderPayType(){
      if(this.orderId && this.orderCode){
        this.$message({
          message: "订单id和流水号填其中一个就行了!!!",
          type: "error"
        });
        return;
      }
      this.$http({
        url:BASE_URL+"v1/call/merge-order",
        method:"GET",
        params:{
          orderId:this.orderId,
          code:this.orderCode,
          channel:this.payType
        }
      }).then(res=>{
        if(res.data.code == 200){
          this.$message({
            message: res.data.msg,
            type: "success"
          });
        }else if(res.data.code == 400){
          this.$message({
            message: res.data.msg,
            type: "error"
          });
        }
      });
    },
    update(){
      this.$confirm('确认此操作？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).then(() => {
        this.$http({
          url: BASE_URL+"v1/clone/update-net",
          method: "GET",
          params: {
            storeId: this.storeIdForUpdateNet,
          }
        }).then(res => {
          this.$message({
            message: "更新成功",
            type: "success"
          });
        });
      })
    },
    saveForRent() {
      this.$confirm('确认复制租赁商品？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).then(() => {
        this.$http({
          url: BASE_URL+"v1/call/clone",
          method: "POST",
          data: {
            toStoreId: this.toStoreIdForRent,
            storeId: this.fromStoreIdForRent
          }
        }).then(res => {
          this.$message({
            message: "操作成功",
            type: "success"
          });
        });
      })
    },
    save() {
      this.$confirm('确认此操作？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).then(() => {
        this.$http({
          url: BASE_URL+"v1/clone/product",
          method: "GET",
          params: {
            storeId: this.toStoreId,
            parentStoreId: this.fromStoreId
          }
        }).then(res => {
          this.$message({
            message: "克隆成功",
            type: "success"
          });
        });
      })
    },
    del() {
      this.$confirm('确认此操作？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).then(() => {
        // 
        this.$http({
          url: BASE_URL+"v1/clone/delete-product",
          method: "GET",
          params: {
            storeId: this.storeIdForDel
          }
        }).then(res => {
          this.$message({
            message: "删除成功",
            type: "success"
          });
        });
      })
    }
  },
  created: function() {},
  mounted() {}
};
</script>
<style lang="scss" scoped>
@import "./cloneStore.scss";
</style>
